package com.nageoffer.shortlink.admin.toolkit;

import java.security.SecureRandom;

/**
 * ClassName: RandomGenerator
 * Package:
 * Description:   分组ID随机生成器
 *
 * @Author:xiaohei
 * @Create 2024/6/5 9:02
 * Version 1.0
 */
public final class RandomGenerator {
    private static final String CHAR_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final SecureRandom random = new SecureRandom();


    /**
     * 生成随机分组ID
     * @return
     */
    public static String generateRandom() {
        return generateRandom(6);
    }

    /**
     * 生成随机分组ID
     * @param length    生成的位数
     * @return
     */
    public static String generateRandom(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be a positive integer");
        }

        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(CHAR_SET.charAt(random.nextInt(CHAR_SET.length())));
        }
        return sb.toString();
    }
}
